using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Chest : MonoBehaviour
{
    public GameObject enemy;
    public GameObject[] guns_0;
    public GameObject[] drop;
    public GameObject imformation;
    public int chestNum;
    private Collider2D coll;
    private Animator anim;
    public LayerMask playerLayer;
    private int number;
    void Start()
    {
        coll = GetComponent<Collider2D>();
        anim = GetComponent<Animator>();
        imformation.SetActive(false);
    }
    void Update()
    {
        if (coll.IsTouchingLayers(playerLayer) && anim.GetBool("open") == false) 
        {
            imformation.SetActive(true);
            if (chestNum == 0)
            {
                anim.SetBool("open", true);
                for(int i = 0; i < 2; i++)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        Vector3 direction = new Vector2(Random.Range(-100, 100), Random.Range(-50, 50)).normalized;
                        GameObject coin = Instantiate(drop[j], transform.position + direction, transform.rotation);
                    }
                }
            }
            else if (Input.GetKeyDown(KeyCode.E))
            {
                anim.SetBool("open", true);
                if (chestNum == 1)
                {
                    number = Random.Range(1, 6);
                    GameObject gun = Instantiate(guns_0[number], transform.position, transform.rotation);
                }
                else if(chestNum == 2)
                {
                    number = Random.Range(1, 10);
                    GameObject gun = Instantiate(guns_0[number], transform.position, transform.rotation);
                }
                else if (chestNum == -1)
                {
                    Invoke("Change", 1.5f);
                }
            }
        }
        else
        {
            imformation.SetActive(false);
        }
    }
    void Change()
    {
        enemy.SetActive(true);
        Destroy(gameObject);
    }

}